﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Collections;
using System.Web;


namespace Utility
{
    public class MyUtility
    {
        public static string GetJsonString(DataTable dt)
        {
            string res = "";
            IList<Hashtable> mList = new List<Hashtable>();
            try
            {
                foreach (DataRow row in dt.Rows)
                {
                    Hashtable ht = new Hashtable();
                    foreach (DataColumn col in dt.Columns)
                    {
                        ht.Add(col.ColumnName, row[col.ColumnName]);
                    }
                    mList.Add(ht);
                }

                //使用了NewtonSoft.Json库中的方法.
                res = Newtonsoft.Json.JsonConvert.SerializeObject(mList);
            }
            catch (Exception ee)
            {
                string error = ee.Message;
            }

            return res;
        }

        public static void SaveAllFiles(HttpFileCollection imgs, ref string carFront, ref string carBack, ref string ph)
        {
            try
            {
                if (imgs.Count > 0)
                {
                    for (int i = 0; i < imgs.Count; i++)
                    {
                        if (imgs[i].ContentLength != 0)
                        {
                            string oldName = imgs[i].FileName.ToString();
                            string imgName = Guid.NewGuid().ToString();
                            string imgType = oldName.ToString().Substring(oldName.LastIndexOf("."));
                            string imgPath = "~/UploadImgs/" + imgName + imgType;
                            imgs[i].SaveAs(HttpContext.Current.Server.MapPath(imgPath));
                            if (HttpContext.Current.Request.Files["IdentityCardFront"].FileName == imgs[i].FileName)
                                carFront = imgName + imgType;
                            if (HttpContext.Current.Request.Files["IdentityCardBack"].FileName == imgs[i].FileName)
                                carBack = imgName + imgType;
                            if (HttpContext.Current.Request.Files["Photo"].FileName == imgs[i].FileName)
                                ph = imgName + imgType;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                
            }
        }

        public static string Path()
        {
            return HttpContext.Current.Server.MapPath("./");
        }
    }
}
